<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\admin\model\Nav;
use app\admin\model\Notice;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('获取成功', [
            'gonggaoqu' => $this->notice(),     //获取首页公告区
        ], 1);
    }

    /**
     * 首页--金刚区
     * 1=首页,2=新房,3=租房
     */
    public function nav()
    {
        //1=首页,2=新房,3=租房
        $navType = input('navType/d', 1);
        if (empty($navType)) {
            $this->error('请选择类型', 0);
        }
        if ($navType == 1) {
            $data = Nav::where(['status' => 1, 'type' => $navType])
                ->field('id,name')
                ->limit(0, 8)
                ->select();
            $this->success('获取成功', $data, 1);
        } elseif ($navType == 2) {
            $data = Nav::where(['status' => 1, 'type' => $navType])
                ->field('id,name')
                ->limit(0, 3)
                ->select();
            $this->success('获取成功', $data, 1);
        } elseif ($navType == 3) {
            $data = Nav::where(['status' => 1, 'type' => $navType])
                ->field('id,name')
                ->limit(0, 4)
                ->select();
            $this->success('获取成功', $data, 1);
        } else {
            $this->error('请求错误', 0);
        }
    }

    /**
     * 首页--轮播
     * 1=首页,2=新房,3=二手房,4=租房
     */
    public function banner()
    {
        //1=首页,2=新房,3=二手房,4=租房
        $bannerType = input('bannerType/d', 1);
        if (empty($bannerType)) {
            $this->error('请选择类型', 0);
        }
        $data = Banner::where(['status' => 1, 'type' => $bannerType])
            ->field('id,image')
            ->limit(0, 3)
            ->select();
        foreach ($data as &$v) {
            $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
        }
        $this->success('获取成功', $data, 1);
    }

    /**
     * 首页--公告区
     *
     */
    public function notice()
    {
        $model = new Notice();
        $data = $model->where(['status' => 1])
            ->field('id,titleone,titletwo,image')
            ->find();
        if (!empty($data)) {
            $data['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $data['image']));
        }
        return $data;
    }

}
